﻿using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Database;
using Android.Database.Sqlite;
using System.IO;
using Android.Util;

namespace KnowledgeInput
{
    [Activity(Label = "Tree")]
    public class Tree : Activity
    {
        LinearLayout content_layout = null;
        string db_path = "";
        string tag = "Tree";
        int id = -1;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            this.SetContentView(Resource.Layout.tree);

            this.id = Intent.GetIntExtra("id", -1);
            this.db_path = Intent.GetStringExtra("db_path");

            if (id == -1)
            {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.SetTitle("系统错误");
                dialog.SetMessage("无法获取ID值");
                dialog.Show();
                return;
            }

            this.content_layout = FindViewById<LinearLayout>(Resource.Id.tree_layout);

            if (File.Exists(db_path))
            {
                this.RefreshUI();
            }
        }
        private void ChildrenButtonsClick(int article_id, string id, string level = "—")
        {
            string[] columns = new string[] { "id", "node_name", "is_fold" };
            SQLite sql = new SQLite(this, db_path);
            SQLiteDatabase db = sql.ReadableDatabase;
            string selection = "article_id = ? AND parent_node_id = ?";
            string[] args = new string[] { article_id.ToString(), id };
            ICursor cursor = db.Query("node", columns, selection, args, null, null, "id ASC");
            while (cursor.MoveToNext())
            {
                int id_column_index = cursor.GetColumnIndex("id");
                int name_column_index = cursor.GetColumnIndex("node_name");
                int is_fold_column_index = cursor.GetColumnIndex("is_fold");
                Button button = new Button(this);
                button.SetTag(Resource.Integer.id, cursor.GetString(id_column_index));
                button.Text = level + cursor.GetString(name_column_index);
                button.SetAllCaps(false);
                button.Click += this.OnButtonsClick;
                button.LongClick += this.OnButtonsLongClick;
                content_layout.AddView(button);
                int is_fold = cursor.GetInt(is_fold_column_index);
                if (is_fold == 0)
                {
                    this.ChildrenButtonsClick(article_id, cursor.GetString(id_column_index), level + "—");
                }
            }
            cursor.Close();
            sql.Close();
            db.Close();
        }
        private void OnButtonsClick(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            string id = b.GetTag(Resource.Integer.id).ToString();
            Intent intent = new Intent(this, typeof(Textbook));
            intent.PutExtra("id", id);
            intent.PutExtra("db_path", db_path);
            this.StartActivity(intent);
        }
        private void OnButtonsLongClick(object sender, EventArgs e)
        {
            Log.Info(tag, "94");
            Button b = (Button)sender;
            string id = b.GetTag(Resource.Integer.id).ToString();
            SQLite sql = new SQLite(this, db_path);
            SQLiteDatabase read_db = sql.ReadableDatabase;
            SQLiteDatabase write_db = sql.WritableDatabase;
            Log.Info(tag, "100");
            string[] columns = new string[] { "is_fold" };
            string selection = "`id` = ?";
            string[] args = new string[] { id };
            ICursor cursor = read_db.Query("node", columns, selection, args, null, null, "id ASC", "1");
            while (cursor.MoveToNext())
            {
                int is_fold_index = cursor.GetColumnIndex("is_fold");
                int is_fold = cursor.GetInt(is_fold_index);
                if (is_fold == 0)
                {
                    is_fold = 1;
                }
                else
                {
                    is_fold = 0;
                }
                Log.Info(tag, "115");
                Log.Info(tag, "UPDATE `node` " +
                    "SET `is_fold` = " + is_fold.ToString() +
                    " WHERE `id` = " + id);
                write_db.ExecSQL("UPDATE `node` " +
                    "SET `is_fold` = " + is_fold.ToString() +
                    " WHERE `id` = " + id);
                cursor.Close();
                read_db.Close();
                write_db.Close();
                sql.Close();
                this.RefreshUI();
            }
        }
        private void RefreshUI()
        {
            this.content_layout.RemoveAllViews();
            string[] columns = new string[] { "id", "node_name", "is_fold" };
            SQLite sql = new SQLite(this, db_path);
            Log.Info(tag, "52");
            SQLiteDatabase db = sql.ReadableDatabase;
            Log.Info(tag, "54");
            string selection = "article_id = ? AND parent_node_id = ?";
            Log.Info(tag, "56");
            string[] args = new string[] { id.ToString(), "-1" };
            Log.Info(tag, "58");
            ICursor cursor = db.Query("node", columns, selection, args, null, null, "id ASC");
            Log.Info(tag, "60");
            while (cursor.MoveToNext())
            {
                int id_column_index = cursor.GetColumnIndex("id");
                int name_column_index = cursor.GetColumnIndex("node_name");
                int is_fold_column_index = cursor.GetColumnIndex("is_fold");
                Button button = new Button(this);
                button.SetTag(Resource.Integer.id, cursor.GetString(id_column_index));
                button.Text = cursor.GetString(name_column_index);
                button.SetAllCaps(false);
                button.Click += this.OnButtonsClick;
                button.LongClick += this.OnButtonsLongClick;
                content_layout.AddView(button);
                int is_fold = cursor.GetInt(is_fold_column_index);
                if (is_fold == 0)
                {
                    this.ChildrenButtonsClick(id, cursor.GetString(id_column_index));
                }
            }
            cursor.Close();
            db.Close();
            sql.Close();
        }
    }
}
